package com.blog.zip.controller.admin;

import com.blog.zip.mapper.ArticleMapper;
import com.blog.zip.mapper.CategoryMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class AdminCategory {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @GetMapping("/admin/category/get")
    public Map<String, Object> getCategory() {
        List<Map<String, Object>> list = categoryMapper.getCategory();
        Map<String, Object> map = new HashMap();
        map.put("code", 200);
        map.put("data", list);
        return map;
        //通过文章的标签找到该文章属于哪个分类的mysql语句
    }

    @PostMapping("/admin/category/add")
    public Map<String, Object> addCategory(@RequestBody Map<String, Object> payload) {
        List list = (List) payload.get("tags");
        long time = System.currentTimeMillis();
        payload.put("id", time);
        payload.put("order", 1);
        payload.put("number", list.size());
        categoryMapper.addCategory(payload);
        log.info("{}", list);
        Map<String, Object> map = new HashMap();
        // 在b3_solo_category_tag 插入目录对应的
        for (Object o : list) {
            long oId = System.currentTimeMillis();
            Map<String, Object> tmp = new HashMap<>();
            long tagId = articleMapper.getTagId(o.toString().trim());
            tmp.put("id", oId);
            tmp.put("category", time);
            tmp.put("tagId", tagId);
            categoryMapper.addCategoryTag(tmp);
        }
        map.put("code", 200);
        map.put("msg", "add success");
        return map;
    }


    @PostMapping("/admin/category/update")
    public Map<String, Object> updateCategory(@RequestBody Map<String, Object> payload) {
        List list = (List) payload.get("tags");
        payload.put("order", 1);
        payload.put("number", list.size());
        categoryMapper.updateCategory(payload);
        categoryMapper.deleteCategoryTag((String) payload.get("id"));
        // 在b3_solo_category_tag 插入目录对应的
        for (Object o : list) {
            long oId = System.currentTimeMillis();
            Map<String, Object> tmp = new HashMap<>();
            long tagId = articleMapper.getTagId(o.toString().trim());
            tmp.put("id", oId);
            tmp.put("category", payload.get("id"));
            tmp.put("tagId", tagId);
            categoryMapper.addCategoryTag(tmp);
        }

        Map<String, Object> map = new HashMap();
        map.put("code", 200);
        map.put("msg", "update success");
        return map;
    }

    @PostMapping("/admin/category/delete")
    public Map<String, Object> deleteCategory(@RequestBody Map<String, Object> payload) {
        categoryMapper.deleteCategory((String) payload.get("id"));
        Map<String, Object> map = new HashMap();
        map.put("code", 200);
        map.put("msg", "delete success");
        return map;
    }
}
